const express = require('express');
const router = express.Router();
const connection = require("../util/db")
const ResultJson = require("../util/json")
const {formatArray, formatObject} = require("../util/jsonFormat")

/* GET home page. */
router.get('/', function (req, res, next) {
    res.render('power/power-list');
});
router.get("/page", function (req, res) {
    const {page = 1, limit = 5, powerName} = req.query;
    let sql = "select * from power where 1 = 1 ";
    const objs = [];
    if (powerName) {
        sql += "and power_name = ? ";
        objs.push(powerName);
    }
    sql += "order by id desc limit ?,?";
    let offset = (page - 1) * limit;
    objs.push(offset, parseInt(limit));
    connection.query(sql, objs, function (e, powerList) {
        if (e) {
            throw e;
        }
        let countSql = "select count(*) count from power where 1 = 1 ";
        const countObjs = [];
        if (powerName) {
            countSql += "and power_name = ? ";
            countObjs.push(powerName);
        }
        connection.query(countSql, countObjs, function (e, r) {
            if (e) {
                throw e;
            }
            res.json(ResultJson.page(r[0].count, formatArray(powerList)));
        })
    })
})

router.get("/edit", function (req, res) {
    const {id} = req.query;
    if (id) {
        connection.query("select * from power where id = ?", id, function (e, r) {
            if (e) {
                throw e;
            }
            res.render("power/power-update", {power: formatObject(r[0])})
        })
    } else {
        res.render("power/power-update", {power: {}})
    }
})

router.post("/update", function (req, res) {
    const {id, powerName, profile} = req.body;
    if (id) {
        connection.query("update power set power_name = ?,profile = ? where id = ?", [powerName, profile, id], function (e, r) {
            if (e) {
                res.json(ResultJson.error(e.message));
            } else {
                res.json(ResultJson.ok());
            }
        })
    } else {
        connection.query("insert into power values(null,?,?)", [powerName, profile], function (e, r) {
            if (e) {
                res.json(ResultJson.error(e.message));
            } else {
                res.json(ResultJson.ok());
            }
        })
    }
})

router.delete("/delete/:id", function (req, res) {
    const id = req.params.id;
    /*先查询要删除的权限是否被使用*/
    connection.query("select * from userinfo where power_id = ?", id, function (e, r) {
        if (e) {
            throw e;
        }
        if (r.length > 0) {
            res.json(ResultJson.error("该权限已被使用，无法删除"))
        } else {
            connection.query("delete from power where id = ?", id, function (e, r) {
                if (e) {
                    res.json(ResultJson.error(e.message));
                } else {
                    res.json(ResultJson.ok());
                }
            })
        }
    })
})

router.get("/select", function (req, res) {
    connection.query("select * from power order by id desc", [], function (e, r) {
        if (e) {
            throw e;
        }
        const powerList = formatArray(r);
        res.json(ResultJson.data(powerList));
    })
})

module.exports = router;
